package v1

import (
	"gitee.com/pangxianfei/frame/http/controller/gincontroller"
	"gitee.com/pangxianfei/frame/kernel/zone"
	"gitee.com/pangxianfei/frame/library/tmaic"
	"gitee.com/pangxianfei/frame/request"
	"gitee.com/pangxianfei/frame/response"
	"gitee.com/pangxianfei/saas/sysmodel"

	"tmaic/user/models"
)

type User struct {
	Controller.BaseGinController
}

func (uc *User) Info(ctx request.Context) *response.JsonResult {
	if ctx.ScanUserWithJSON() {
		return response.JsonFail("用户不存在")
	}
	PlatformAdmin := ctx.User().Value().(*sysmdel.PlatformAdmin)
	return response.JsonQueryData(PlatformAdmin)
}

// AppDb 测试产品应用，CURD
func (uc *User) AppDb(ctx request.Context) *response.JsonResult {
	//获取DB对象 方法一 通过上下文应用ID
	AppId := ctx.GetInt64("AppId")
	TenantId := ctx.GetInt64("TenantId")
	db := ctx.Db()
	//创建
	newProduct := models.Product{Name: "产品一" + zone.Now().String()}
	db.Create(&newProduct)
	//查询
	var selectProduct models.Product
	db.First(&selectProduct, newProduct.ID)
	//更新 方法二 通过上下文应用ID
	ctx.ContextTenantDb().Save(&models.Product{ID: selectProduct.ID, Name: "产品一,更新后的名称" + zone.Now().String()})

	return response.JsonQueryData(tmaic.V{"appid": AppId, "TenantId": TenantId, "selectProduct": selectProduct, "newProduct": "newProduct"})
}
